using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AccountingLibs;

namespace BasicForms
{
    public enum TextFormatType
    {
        Code,
        Integer,
        Money
    }

    public partial class FrmBase : DevExpress.XtraEditors.XtraForm
    {
        public static void InitGridLookUpEdit(DevExpress.XtraGrid.Views.Grid.GridView grdView, string columnName,
              DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit gItemGridLookUp, object dataSouce,
              string keyColName, string valueColName, string keyCaption, string valueCaption)
        {
            grdView.Columns[columnName].ColumnEdit = gItemGridLookUp;
            gItemGridLookUp.DataSource = dataSouce;
            gItemGridLookUp.NullText = "";
            foreach (DevExpress.XtraGrid.Columns.GridColumn col in gItemGridLookUp.View.Columns)
            {
                if (col.Name == keyColName)
                {
                    col.Visible = true;
                    col.Caption = keyCaption;
                }
                else if (col.Name == valueColName)
                {
                    col.Visible = true;
                    col.Caption = valueCaption;
                }
                else
                {
                    col.Visible = false;
                }
            }
            gItemGridLookUp.View.BestFitColumns();
        }

        public FrmBase()
        {
            InitializeComponent();
        }

        private void FrmBase_Load(object sender, EventArgs e)
        {
            FormatControls();
        }

        public static void FormatTextEdit(DevExpress.XtraEditors.TextEdit textEdit, TextFormatType textFormatType)
        {
            switch (textFormatType)
            {
                case TextFormatType.Code:
                    textEdit.Properties.Mask.EditMask = @"(\P{Ll}*)+";
                    textEdit.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.RegEx;
                    break;
                case TextFormatType.Integer:
                    textEdit.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;

                    textEdit.Properties.Mask.EditMask = "n0";
                    textEdit.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
                    break;
                case TextFormatType.Money:
                    textEdit.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;

                    textEdit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    //textEdit.Properties.DisplayFormat.FormatString = AccountingLibs.LocalSettings.HeThong.NumbericFormatString;
                    textEdit.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    //textEdit.Properties.EditFormat.FormatString = AccountingLibs.LocalSettings.HeThong.NumbericFormatString;
                    textEdit.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
                    textEdit.Properties.Mask.EditMask = AccountingLibs.LocalSettings.HeThong.NumbericFormatString;

                    textEdit.Properties.DisplayFormat.FormatString = AccountingLibs.LocalSettings.HeThong.NumbericFormatString; ;
                    textEdit.Properties.EditFormat.FormatString = AccountingLibs.LocalSettings.HeThong.NumbericFormatString;
                    break;
            }
        }

        public static void FormatNumberInGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            foreach (DevExpress.XtraGrid.Columns.GridColumn column in gridView.Columns)
            {
                if (column.ColumnType.Name == "Decimal")
                {
                    column.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                    column.DisplayFormat.FormatString = AccountingLibs.LocalSettings.HeThong.NumbericFormatString;
                    //column.DisplayFormat.FormatString = "###.###,###";
                }
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            switch (keyData)
            {
                //case Keys.Escape:
                //    this.Close();
                //    break;
                case Keys.F1:
                    AccountingLibs.CommonLib.ShowHelp(this.Name);
                    return true;
                    break;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }


        protected virtual void FormatControls()
        {
            foreach (Control control in this.Controls)
            {
                FormatControlsRecuirement(control);
            }
        }
        private void FormatControlsRecuirement(Control control)
        {
            foreach (Control childControl in control.Controls)
            {
                FormatControlsRecuirement(childControl);
            }
            if (control is DevExpress.XtraEditors.DateEdit)
            {
                (control as DevExpress.XtraEditors.DateEdit).Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                (control as DevExpress.XtraEditors.DateEdit).Properties.DisplayFormat.FormatString = "dd/MM/yyyy HH:mm";
                (control as DevExpress.XtraEditors.DateEdit).Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                (control as DevExpress.XtraEditors.DateEdit).Properties.EditFormat.FormatString = "dd/MM/yyyy HH:mm";
                (control as DevExpress.XtraEditors.DateEdit).Properties.EditMask = "dd/MM/yyyy HH:mm";
            }
        }
    }
}